package com.lankystar.sos.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lankystar.sos.dao.IResponseDao;
import com.lankystar.sos.dao.ISosOrderDao;
import com.lankystar.sos.dao.IStatusDao;
import com.lankystar.sos.dao.IUserDao;
import com.lankystar.sos.model.ResponseOrder;
import com.lankystar.sos.model.ResponseStatus;
import com.lankystar.sos.model.SosOrder;
import com.lankystar.sos.model.SosStatus;
import com.lankystar.sos.model.User;
import com.lankystar.sos.model.UserType;
import com.lankystar.sos.service.IResponseInterface;

@Service
@Transactional
public class ResponseInterface implements IResponseInterface{

	@Autowired
	private IResponseDao responseDao;
	@Autowired
	private IUserDao userDao;
	@Autowired
	private IStatusDao statusDao;
	@Autowired
	private ISosOrderDao sosDao;
	@Override
	public Map<String, Object> addResponseMethod(Integer userId, Integer sosId, Integer respkey) {
		Object[] ob = statusDao.selectSosStatusBysosId(sosId);
		SosOrder sosOrder = (SosOrder) ob[0];
		SosStatus sos = (SosStatus) ob[1];
		Map<String, Object> map = new HashMap<String, Object>();
		/**
		 * user
		 */
		Map<String, Object> usermap = new HashMap<>();
		User user = userDao.selectUserById(userId);//查到施救者信息
		usermap.put("userId", user.getUserId());
		usermap.put("userPhone", user.getUserPhone());
		usermap.put("userPortrait", user.getUserPortrait());
		usermap.put("userEmail", user.getUserEmail());
		usermap.put("userGender", user.getUserGender());
		map.put("user", usermap);
		/**
		 * 更新求救单状态
		 */
		SosStatus sosStatus = statusDao.selectSosStatusBysoskey(respkey);
		sosOrder.setSosStatus(sosStatus);
		sosDao.updateSosOrder(sosOrder);
		/**
		 * 生成响应单
		 */
		ResponseStatus respStatus = statusDao.selectResponseStatusByrespkey(respkey);
		ResponseOrder respOrder = new ResponseOrder();
		respOrder.setCreateTime(new Date());
		respOrder.setLastUpdateTime(new Date());
		respOrder.setSosOrder(sosOrder);
		respOrder.setUser(user);
		respOrder.setRespStatus(respStatus);
		respOrder.setPushCount(1);
		responseDao.addResponse(respOrder);
		Map<String, Object> respmap = new HashMap<>();
		respmap.put("lastUpdateTime", respOrder.getLastUpdateTime());
		respmap.put("responseId", respOrder.getResponseId());
		respmap.put("respkey", respStatus.getRespkey());
		respmap.put("respvalue", respStatus.getRespvalue());
		map.put("responseOrder", respmap);
		return map;
	}
	@Override
	public Map<String, Object> updateResponseMethod(Integer responseId, Integer respkey) {
		/**
		 * 同步更新求救单   和   响应单
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		SosStatus sosStatus = statusDao.selectSosStatusBysoskey(respkey);//查找求救单状态
		ResponseStatus respStatus = statusDao.selectResponseStatusByrespkey(respkey);//查找响应单状态
		Object[] ob = responseDao.selectRespOrderAndSosOrder(responseId);
		ResponseOrder resp = (ResponseOrder) ob[0];
		ResponseStatus respsta = (ResponseStatus) ob[1];
		User respUser = (User) ob[2];
		UserType userType = (UserType) ob[4];
		SosOrder sos = (SosOrder) ob[5];
		User sosUser = (User) ob[6];
		/**
		 * 判断响应单状态改变方向
		 */
		if(respsta.getRespkey()<respkey){
			/**
			 * 更新求救单、响应单
			 */
			resp.setRespStatus(respStatus);
			resp.setLastUpdateTime(new Date());
			responseDao.updateResponse(resp);
			sos.setSosStatus(sosStatus);
			sosDao.updateSosOrder(sos);
			/**
			 * 分开保存
			 */
			
			Map<String, Object> respmap = new HashMap<>();
			respmap.put("responseId", resp.getResponseId());
			respmap.put("lastUpdateTime", resp.getLastUpdateTime());
			respmap.put("respkey", respStatus.getRespkey());
			respmap.put("respvalue", respStatus.getRespvalue());
			respmap.put("userId", respUser.getUserId());
			respmap.put("userPhone", respUser.getUserPhone());
			respmap.put("userGender", respUser.getUserGender());
			respmap.put("uservalue", userType.getUservalue());
			respmap.put("userPortrait", respUser.getUserPortrait());
			
			
			Map<String, Object> sosmap = new HashMap<>();
			sosmap.put("sosId", sos.getSosId());
			sosmap.put("soskey", sosStatus.getSoskey());
			sosmap.put("sosvalue", sosStatus.getSosvalue());
			sosmap.put("userId", sosUser.getUserId());
			sosmap.put("userPhone", sosUser.getUserPhone());
			sosmap.put("userGender", sosUser.getUserGender());
			sosmap.put("userPortrait", sosUser.getUserPortrait());
			
			map.put("responseOrder", respmap);
			map.put("sosOrder", sosmap);
		}else{
			map.put("err", "响应单状态不可逆向改变！！");
		}
		return map;
	}

}
